from django.db import models

from fbase.models import Object
from .basemodels import RecordBase
from utils.timezone import astimezone2cst


class Salary(Object):
    serial_number = models.CharField(max_length=128, verbose_name='序号')
    employee = models.ForeignKey('organization.Employee', on_delete=models.PROTECT, verbose_name='员工姓名')
    payable_salary = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='应发合计')
    actual_salary = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='实发工资')
    endowment_insurance_p = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='个人养老保险')
    unemployment_insurance_p = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='个人事业保险')
    hospitalization_insurance_p = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='个人医疗保险')
    reserve_fund_p = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='个人公积金')
    individual_income_tax = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='代扣个税')
    other_pay = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='其他支付方式')
    fiveone_total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='五险一金合计')
    company_pay_total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='公司总共投入')
    pay_date = models.DateTimeField(verbose_name='发放月份')
    businessline = models.ForeignKey('organization.BusinessLine', on_delete=models.PROTECT, verbose_name='业务线')
    company = models.ForeignKey('organization.Company', on_delete=models.PROTECT, verbose_name='公司')
    businessdept = models.ForeignKey('organization.BusinessDept', on_delete=models.PROTECT, verbose_name='事业部')

    class Meta:
        verbose_name_plural = '薪资表'
        verbose_name = verbose_name_plural

    def __str__(self):
        return '{}:{}'.format(
            self.employee.name,
            astimezone2cst(self.pay_date)
        )


class IncomeRecord(RecordBase):
    income = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='收益')

    class Meta:
        verbose_name_plural = '收入记录表'
        verbose_name = verbose_name_plural


class BusinessCostRecord(RecordBase):
    cost = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='成本')

    class Meta:
        verbose_name_plural = '业务成本记录表'
        verbose_name = verbose_name_plural
